<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link href="../css/own.css" rel="stylesheet" />
		<link rel="stylesheet" href="../css/mui.picker.min.css" />
		<style type="text/css">
			.st_cot {
				font-size: 0.65rem;
				line-height: 0.7rem;
				background: #FFFFFF;
				border: none;
				width: 70%;
				color: #50CEBD;
			}
			
			.mui-table-view-cell {
				line-height: 1.4rem;
			}
			
			.mui-table-view-cell.mui-active {
				background: transparent;
			}
			
			.value-holder {
				color: #333333;
				padding-left: 0.3rem;
			}
			
			#div_button {
				background: #007aff;
				color: #ffffff;
				font-size: 0.8rem;
				padding: 0.5rem 1rem;
				border-radius: 0.7rem;
				margin: 2rem 0.5rem;
				text-align: center;
			}
			
			.mui-table-view-cell span {
				font-size: 0.7rem;
			}
			
			.zt_size {
				font-size: 0.75rem;
				/* margin-left:18%; */
			}
			
			span.zt_size {
				font-size: 0.75rem;
			}
			
			#zt_span {
				height: 1.6rem;
				display: inline-block;
				background: #007aff;
				color: #ffffff;
				font-size: 0.65rem;
				padding: 0 0.1rem;
				border-radius: 0.2rem;
				line-height: 1.8rem;
				width: 3rem;
				margin-left: 1rem;
				text-align: center;
				
			}
			
			input.zb_ys {
				color: #333333;
				width: 80%;
				height: 1.8rem;
				padding: 0rem 0rem;
				margin: 0rem;
				font-size: 0.65rem;
				text-align: center;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar own-mui-bar-nav own-main-background-color ">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left "></a>
			<h1 class="mui-title">新增单元工程</h1>
			<span class="mui-icon mui-icon-location  mui-pull-right" id="zuobiao" style="font-size: 0.7rem;line-height: 1.2rem;color: #FFFFFF">获取坐标</span>
		</header>
		<div class="mui-content">
			<ul class="mui-table-view " style="font-size: 0.65rem;">
				<li class="mui-table-view-cell">
					<span style="font-size: 0.75rem;">单元工程编号:</span>
					<input id="uproj_num" class="value-holder st_cot" placeholder="请输入单元工程编号" />
				</li>
				<li class="mui-table-view-cell">
					<span style="font-size: 0.75rem;">单元工程名称:</span>
					<input id="uproj_name" class="value-holder st_cot" placeholder="请输入单元工程名称"></input>
				</li>
				<li class="mui-table-view-cell">
					<a class="mui-navigate-right" id="a_unit">
						<span class="zt_size">工艺:</span>
						<div id="engn_unit" class="value-holder mui-inline st_cot" style="line-height: 1.4rem;"></div>
						<div id="engn_vul" style="display: none;"></div>
					</a>
				</li>

				<li class="mui-table-view-cell" >
					<div class="mui-inline zt_size">厚度:</div>
					<div class="value-holder mui-inline">
						<input id="dyhd" type="number" style="color: #333333;width: 80%;height:1rem;padding:0rem 0rem;margin:0rem ;border: none;font-size: 0.65rem;" placeholder="请输入单元厚度" />
					</div>

				</li>
				<li class="mui-table-view-cell">
					<div class="mui-inline"  id="zbdata">
						<div class="mui-inline mui-text-right" style="width: 15%;margin-top: 0.3rem;" id="addzb">
							<span id="zt_span" class="mui-icon mui-icon-plus">添加点</span>
						</div>

					</div>

				</li>
			</ul>

			<div id="div_button">保存</div>
		</div>

		<script src="../js/mui.min.js"></script>
		<script src="../js/common.js"></script>
		<script src="../js/jquery-1.7.2.min.js"></script>
		<script src="../js/mui.picker.min.js"></script>
		<script src="../js/BluetoothTool.js"></script>
		

		<script type="text/javascript">
			mui.init();

			var indexview = null;

			var nodestr = null;

			var num = 1;
			
			var wo = null;
			/* 蓝牙 */
			var bluetoothTool = null;
			
			var receiveDataArr =[];
			
			var jingweidu = null;
			
			mui.plusReady(function() {
                 
				 /* 蓝牙 */
				 bluetoothTool = BluetoothTool();
				 
				indexview = plus.webview.currentWebview();
				
				wo = indexview.opener();

				nodestr = indexview.data;

				document.getElementById('div_button').addEventListener('tap', function() {
					savedata();
				});

				document.getElementById('addzb').addEventListener('tap', function() {
					num++;
					setItems(num);
					//添加坐标值
				});
				if(!isNull(nodestr)) {
					setItems(num);
				}

				addEt();
				
				
				
				bluetoothTool.init({
					readDataCallback: function(dataByteArr) {
						if(receiveDataArr.length >= 240) {
							jingweidu =String.fromCharCode.apply(String, receiveDataArr);
							receiveDataArr =[];
						}
						receiveDataArr.push.apply(receiveDataArr, dataByteArr);
					}
				});
				
				
				document.getElementById('zuobiao').addEventListener('tap',function () {
					 plus.nativeUI.showWaiting('获取中...');
					if(bluetoothTool.getPairedDevices().length>0&&bluetoothTool.getPairedDevices()[0].name.indexOf('BDXGX') != -1){
						bluetoothTool.connDevice(bluetoothTool.getPairedDevices()[0].address);
						setTimeout(function() {
							bluetoothTool.disConnDevice();
							console.log(jingweidu);
						if(jingweidu){
						   if(jingweidu.indexOf('$PTNL')!= -1 && jingweidu.indexOf('$KSXT')!= -1){
							   var str01 =jingweidu.split('$KSXT,')[1];
							   var str02 =str01.split(',');
							   if(str02.length>2){
								     var params = {
								     	'node': nodestr, //节点编号
								     	'lon': str02[1], 
								     	'lat': str02[2], 
										'h':str02[3]
								     };
								     			
								     mpms_ajax_noWaiting(params, 'coordTrans', function(data) {
								         if(data.status == '0'){
											 $('#lon'+num).val(data.data.N);
											 $('#lat'+num).val(data.data.E);
											 plus.nativeUI.closeWaiting();
								     	}else{
											plus.nativeUI.closeWaiting();
								     		 mui.toast(data.msg);
								     	}
								     });
							   }else{
								   plus.nativeUI.closeWaiting();
								  mui.toast('请重新获取值'); 
							   }
							   
						   }else{
							   plus.nativeUI.closeWaiting();
							  mui.toast('请重新获取值');  
						   }
						   }else{
							     plus.nativeUI.closeWaiting();
								 mui.toast('请重新获取值'); 
						   }
						}, 5000);
						
					}else{
						plus.nativeUI.closeWaiting();
						mui.toast('蓝牙连接设备错误，请重新连接');
					}
				})
				
			});

			//保存单元工程
			function savedata() {
				getPagetoken();
				var uproj_name = $('#uproj_name').val();
				var uproj_num = $('#uproj_num').val();
				var engn_unit = $('#engn_vul').text(); 
				var dyhd = $('#dyhd').val();
			    var zhz = null;
			   
				if(isNull(uproj_num)) {
					mui.toast('请输入单元工程编号');
					return;
				}
				if(isNull(uproj_name)) {
					mui.toast('请输入单元工程名称');
					return;
				}
				
				if(isNull(engn_unit)) {
					mui.toast('请选择工艺');
					return;
				}
				if(isNull(dyhd)) {
					mui.toast('请输入单元厚度');
					return;
				}
				
				 $("#zbdata input").each(function(i) {
				  	 if(!isNull(this.value)){
				  	 	if(i%2 ==0){
							if(isNull(zhz)){
								zhz = this.value;
							}else{
							  zhz += ';'+this.value	
							}
					     	}else{
					     	  zhz +=','+this.value;	
					     	}
				  	   }
						
				     });
				if(isNull(zhz)){
					mui.toast('请填写坐标数据!');
					return;
				}else{ 
				  var  abc = zhz.split(/[,;]/);
					if(abc.length%2 != 0){
						mui.toast('请将坐标数据填写完整!');
						return;
					}
				}

				var params = {
					'node': nodestr, //节点编号
					'name': uproj_name, //单元名称
					'uid': uproj_num, //单元工程编号
					'thick': dyhd, //厚度
					'tid': engn_unit, //工艺编
					'geo': zhz //坐标 
				};
			
				mpms_ajax(params, 'zoneHandle', function(data) {
				    if(data.status == '0'){
    					 mui.toast('保存成功');
    					 mui.back();
					}else{
						 mui.toast(data.msg);
					}
				});

			}

			//工程量单位选择
			function addEt() {
				popview = new mui.PopPicker();
				document.getElementById('a_unit').addEventListener('tap', function() {
					popview.show(function(dt) {
						if(dt.length > 0) {
							$('#engn_unit').text(dt[0].text);
							$('#engn_vul').text(dt[0].value);

						}

					});
				});

				mpms_ajax_noWaiting({
					node: nodestr
				}, 'getTechList', function(data) {

					if(data.list.length > 0) {
						var job = null;
						var jobList = [];
						for(let vals of data.list) {
							job = new Object();
							job.value = vals.id;
							job.text = vals.name;
							jobList.push(job);

						}
						popview.setData(jobList);
					}
				});
			}
			function setItems(i) {
				var htmls = '';
              if(i !=1){
				 htmls += '<div class="mui-inline mui-text-right" style="width: 25%;margin-top: 0.1rem;visibility: hidden;" ><span id="zt_span" class="mui-icon mui-icon-plus">添加点</span></div>'
				 htmls += ' <div class="mui-inline zt_size" style="margin-top: 0.6rem;visibility: hidden;">坐标:</div>';
              }else{
				 htmls += ' <div class="mui-inline zt_size" style="margin-top: 0.6rem;">坐标:</div>';	
				}
				
				htmls += '<div class="mui-inline" style="width: 30%;margin-top: 0.2rem;"><input type="number" class="zb_ys"   id="lon'+i+'"/></div>';
				htmls += '<div class="mui-inline" style="width: 30%;margin-top: 0.2rem;"> <input  type="number"  class="zb_ys"  id="lat'+i+'"/></div>';
				$('#addzb').before(htmls);
			}
			
		
		</script>
	</body>

</html>